package com.hongkuncheng.cms.helper;

import org.hibernate.FactorySession;
import org.springframework.stereotype.Component;
import com.hongkuncheng.cms.model.Article;
import com.hongkuncheng.cms.model.ArticleReColumns;
import com.hongkuncheng.cms.model.Columns;
import com.hongkuncheng.cms.model.Zhuanti;

/**
 * 文章帮助类
 *
 * @author 洪坤成
 */
@Component
public class ArticleHelper {

    public static FactorySession factorySession;

    /**
     * 获取文章详细地址
     *
     * @param article
     * @return
     */
    public static String getDetailLink(Article article) {
        return getDetailLink(article, "");
    }

    /**
     * 获取文章详细地址
     *
     * @param article
     * @param urldirectory
     * @return
     */
    public static String getDetailLink(Article article, String urldirectory) {
        String urllink = null;
        if (article.getExturl() != null && !article.getExturl().isEmpty()) {
            urllink = article.getExturl();
        } else {
            Columns columns = null;
            if (urldirectory == null || urldirectory.isEmpty()) {
                for (ArticleReColumns arc : article.getArticleReColumnses()) {
                    columns = arc.getColumns();
                    break;
                }
                urldirectory = columns.getUrldirectory();
                if (columns.getParentId() == 0) {
                    if (columns.getZhuanti() != null)
                        urldirectory = columns.getZhuanti().getUrldirectory() + "/" + urldirectory;
                } else {
                    Columns parent = (Columns) factorySession.get(Columns.class, columns.getParentId());
                    if (parent.getColumnsType().getId() < 4)
                        urldirectory = parent.getUrldirectory() + "/" + urldirectory;
                }
            } else {
                boolean isContains = false;
                for (ArticleReColumns arc : article.getArticleReColumnses()) {
                    if (arc.getColumns().getUrldirectory().equals(urldirectory)) {
                        isContains = true;
                        columns = arc.getColumns();
                        break;
                    }
                }
                if (isContains) {
                    if (columns.getParentId() > 0) {
                        urldirectory = columns.getUrldirectory();
                        Columns parent = (Columns) factorySession.get(Columns.class, columns.getParentId());
                        if (parent.getColumnsType().getId() < 4)
                            urldirectory = parent.getUrldirectory() + "/" + urldirectory;
                        if (columns.getZhuanti() != null)
                            urldirectory = columns.getZhuanti().getUrldirectory() + "/" + urldirectory;
                    }
                } else {
                    for (ArticleReColumns arc : article.getArticleReColumnses()) {
                        columns = arc.getColumns();
                        break;
                    }
                    urldirectory = columns.getUrldirectory();
                    if (columns.getParentId() == 0) {
                        if (columns.getZhuanti() != null)
                            urldirectory = columns.getZhuanti().getUrldirectory() + "/" + urldirectory;
                    } else {
                        Columns parent = (Columns) factorySession.get(Columns.class, columns.getParentId());
                        if (parent.getColumnsType().getId() < 4)
                            urldirectory = parent.getUrldirectory() + "/" + urldirectory;
                    }
                }
            }
            urllink = "/" + urldirectory + "/detail/" + article.getId() + ".html";
        }
        return urllink;
    }

    /**
     * 获取文章详细地址
     *
     * @param article
     * @param columns
     * @return
     */
    public static String getDetailLink(Article article, Columns columns) {
        String urllink = null;
        if (article.getExturl() != null && !article.getExturl().isEmpty()) {
            urllink = article.getExturl();
        } else {
            String urldirectory = columns.getUrldirectory();
            boolean isContains = false;
            for (ArticleReColumns arc : article.getArticleReColumnses()) {
                if (arc.getColumns().getUrldirectory().equals(urldirectory)) {
                    isContains = true;
                    columns = arc.getColumns();
                    break;
                }
            }
            if (isContains) {
                if (columns.getParentId() > 0) {
                    urldirectory = columns.getUrldirectory();
                    Columns parent = (Columns) factorySession.get(Columns.class, columns.getParentId());
                    if (parent.getColumnsType().getId() < 4)
                        urldirectory = parent.getUrldirectory() + "/" + urldirectory;
                    if (columns.getZhuanti() != null)
                        urldirectory = columns.getZhuanti().getUrldirectory() + "/" + urldirectory;
                }
            } else {
                for (ArticleReColumns arc : article.getArticleReColumnses()) {
                    columns = arc.getColumns();
                    break;
                }
                urldirectory = columns.getUrldirectory();
                if (columns.getParentId() == 0) {
                    if (columns.getZhuanti() != null)
                        urldirectory = columns.getZhuanti().getUrldirectory() + "/" + urldirectory;
                } else {
                    Columns parent = (Columns) factorySession.get(Columns.class, columns.getParentId());
                    if (parent.getColumnsType().getId() < 4)
                        urldirectory = parent.getUrldirectory() + "/" + urldirectory;
                }
            }
            urllink = "/" + urldirectory + "/detail/" + article.getId() + ".html";
        }
        return urllink;
    }

    /**
     * 获取文章详细地址
     *
     * @param article
     * @param urldirectory
     * @return
     */
    public static String getDetailLink(Article article, Zhuanti zhuanti, String urldirectory) {
        String urllink = null;
        if (article.getExturl() != null && !article.getExturl().isEmpty()) {
            urllink = article.getExturl();
        } else {
            urllink = "/" + zhuanti.getUrldirectory() + "/" + urldirectory + "/detail/" + article.getId() + ".html";
        }
        return urllink;
    }

    /**
     * 获取文档分页内容
     *
     * @param contents
     * @return
     */
    public static String GetPageContents(String contents) {
        String[] contentses = contents.split("_ueditor_page_break_tag_");
        if (contentses.length > 1) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < contentses.length; i++) {
                String content = contentses[i];
                result.append("<div id=\"pagebreakafter" + i + "\" class=\"pagebreakafter\"");
                if (i > 0) result.append(" style=\"display:none\"");
                result.append(">");
                result.append(content);
                result.append("</div>");
            }
            result.append("<br class=\"clear\" /><br /><div class=\"pager\">");
            for (int i = 0; i < contentses.length; i++) {
                result.append("<a href=\"javascript:setpagebycontents(" + i + ")\"");
                if (i == 0) result.append(" class=\"pcs\"");
                result.append(">" + (i + 1) + "</a>");
                if (i < contentses.length - 1) result.append(" ");
            }
            result.append("</div>");
            return result.toString();
        } else {
            return contents;
        }
    }

}
